<?php

header('Content-Type:application/json; charset=utf-8'); #返回json
session_start();

if(empty($_SESSION['userinfo'])){
    //未登录，引导登录
    $result = [
        'code' => '-1',
        'msg' => '未登录，请登录',
    ];
    echo json_encode($result);
    exit ;

}

$input = file_get_contents('php://input'); 
// echo $input;
$arr = json_decode($input, true);  //转化为普通数据
$work_id=  $arr["work_id"] ;
$stu_id=  $arr["stu_id"] ;
$sub_content=  $arr["sub_content"];
$sub_img1= $arr["sub_img1"];
$sub_img2= $arr["sub_img2"]??'';


        
try {
    $dbh = new PDO("mysql:host=127.0.0.1;dbname=g09short", 'root', 'G09short.'); //初始化一个PDO对象
    $sql="SELECT * from zyh_submit where work_id=".$work_id." AND stu_id= ".$stu_id;
    $stmt = $dbh->query($sql,    PDO::FETCH_ASSOC);

    if($stmt -> rowCount() == 0){
        $sql = "INSERT INTO zyh_submit(`stu_id`, `work_id`, `sub_content`, `stu_creatime`, `sub_eval`, `sub_score`,`sub_img1`,`sub_img2`) VALUE(" . $stu_id . "," . $work_id . ",'" . $sub_content . "',NOW(),null,null,'" . $sub_img1 . "','" . $sub_img2 . "')";
        // $dbh->exec($sql);
        $stmt = $dbh->query($sql,    PDO::FETCH_ASSOC);
    }
    $dbh=null;
    if ($stmt){
        $res= [
            'code' => '0',
            'msg' => '上传成功',
        ];
    }
    else{
        
        $res= [
            'code' => '2',
            'msg' => '上传失败',
            'stu_id' => $stu_id
        ];
    }

} catch (PDOException $e) {
    
    $res=  [
        'code' => '5',
        'msg' => '出错',
    ];
}


//返回json
echo json_encode($res);
